﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

using QuickDev.Model;
using QuickDev.Common.DB;
using QuickDev.Common.Utils;
using QuickDev.Common.Serialization;

#endregion

namespace QuickDev.SQLServerDAL
{
    /// <summary>
    /// Data Access Layer For dbo.SYS_ServiceConfiguration.
    /// </summary>
    public class ServiceConfigurationDAL
    {
        #region Constants

        private const string CONFIGURATION_SERVICE = "Configuration Service Name";

        #endregion

        #region IsExist

        /// <summary>
        /// public static bool IsExist( string service , string type )
        /// </summary>
        /// <param name="service">service</param>
        /// <param name="type">type</param>
        /// <returns>bool Existed</returns>
        public static bool IsExist( string service, string type )
        {
            string procedureName = "SYS_ServiceConfiguration_IsExist";

            SqlParameter[] parameters = {
					new SqlParameter("@Service", SqlDbType.VarChar,50),
                    new SqlParameter("@Type", SqlDbType.VarChar,500),
                    new SqlParameter()
				};
            parameters[0].Value = service;
            parameters[1].Value = type;
            parameters[2].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure, procedureName, parameters );
                return ( (int)( parameters[2].Value ) == 0 ? false : true );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }

        #endregion

        #region GetModel

        /// <summary>
        /// public ServiceConfiguration GetModel(int configurationId)
        /// </summary>
        /// <param name="configurationId">configurationId</param>
        /// <returns>ServiceConfiguration model</returns>
        public static ServiceConfiguration GetModel( int configurationId )
        {
            string procedureName = "SYS_ServiceConfiguration_GetModel";

            ServiceConfiguration model = new ServiceConfiguration();

            SqlParameter parameter = new SqlParameter( "@ConfigurationId", configurationId );

            try
            {
                ReaderFactory.ReaderToModel<ServiceConfiguration>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure, procedureName, parameter ), model );
                return model;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion

        #region GetModel

        /// <summary>
        /// public ServiceConfiguration GetModel(string service)
        /// </summary>
        /// <param name="service">service</param>
        /// <returns>ServiceConfiguration model</returns>
        public static ServiceConfiguration GetModel( string service )
        {
            string procedureName = "SYS_ServiceConfiguration_FetchModelByService";

            ServiceConfiguration model = new ServiceConfiguration();

            SqlParameter parameter = new SqlParameter( "@Service", service );

            try
            {
                ReaderFactory.ReaderToModel<ServiceConfiguration>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure, procedureName, parameter ), model );
                return model;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion

        #region ServiceConfigurationDAL.FetchConfigurationByService( string service ) Method

        /// <summary>
        /// Fetches the configuration by service.
        /// </summary>
        /// <param name="service">Name of the configuration service.</param>
        /// <returns></returns>
        public static object FetchConfigurationByService( string service )
        {
            Validation.ValidateStringArgumentIsNotNullOrEmptyString( service, CONFIGURATION_SERVICE );

            ServiceConfiguration serviceConfiguration = ServiceConfigurationDAL.GetModel( service );

            object obj = null;

            if ( serviceConfiguration != null && serviceConfiguration.ConfigurationId > 0 )
            {
                Serializer serializer = new Serializer();
                obj = serializer.DeserializeObject( serviceConfiguration.Value, serviceConfiguration.Type );
            }
            return obj;
        }

        #endregion

        #region GetList

        /// <summary>
        /// public List<ServiceConfiguration> FetchConfigurationsByService( string service , int status )
        /// </summary>
        /// <param name="service">service</param>
        /// <param name="status">status</param>
        /// <returns>List<ServiceConfiguration></returns>
        public static List<ServiceConfiguration> FetchConfigurationsByService( string service, int status )
        {
            string procedureName = "SYS_ServiceConfiguration_FetchConfigurationsByService";

            List<ServiceConfiguration> modelList = new List<ServiceConfiguration>();

            SqlParameter[] parameters = {
					new SqlParameter("@Service", SqlDbType.VarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4)
				};
            parameters[0].Value = service;
            parameters[1].Value = status;

            try
            {
                ReaderFactory.ReaderToList<ServiceConfiguration>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure, procedureName, parameters ), modelList );
                return modelList;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion

        #region Add

        /// <summary>
        /// public static int Add( string service, string type, string value, int createBy )
        /// </summary>
        /// <param name="service">service</param>
        /// <param name="type">type</param>
        /// <param name="value">value</param>
        /// <param name="createBy">createBy</param>
        /// <returns>ConfigurationId</returns>
        public static int Add( string service, string type, string value, int createBy )
        {
            if ( IsExist( service, type ) )
                throw new ArgumentException
                    ( String.Format( "{0} for this service configuration is already existed.", type ) );

            string procedureName = "SYS_ServiceConfiguration_ADD";

            SqlParameter[] parameters = {
				new SqlParameter("@Service", SqlDbType.VarChar,50),	
				new SqlParameter("@Type", SqlDbType.VarChar,500),	
				new SqlParameter("@Value", SqlDbType.VarChar,8000),	
				new SqlParameter("@CreateBy", SqlDbType.Int,4),
                new SqlParameter("@ConfigurationId", SqlDbType.Int,4)
			};

            parameters[0].Value = service;
            parameters[1].Value = type;
            parameters[2].Value = value;
            parameters[3].Value = createBy;
            parameters[4].Direction = ParameterDirection.Output;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure, procedureName, parameters );

                return (int)Utility.ChangeTypeTo( parameters[4].Value, typeof( Int32 ) );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion

        #region Update

        /// <summary>
        /// public static bool Update( int configurationId, string service, string type, string value, int updateBy )
        /// </summary>
        /// <param name="configurationId">configurationId</param>
        /// <param name="service">service</param>
        /// <param name="type">type</param>
        /// <param name="value">value</param>
        /// <param name="updateBy">updateBy</param>
        /// <returns>bool successful</returns>
        public static bool Update( int configurationId, string service, string type, string value, int updateBy )
        {
            string procedureName = "SYS_ServiceConfiguration_Update";

            SqlParameter[] parameters = {
				new SqlParameter("@ConfigurationId", SqlDbType.Int,4),	
				new SqlParameter("@Service", SqlDbType.VarChar,50),	
				new SqlParameter("@Type", SqlDbType.VarChar,500),	
				new SqlParameter("@Value", SqlDbType.VarChar,8000),	
				new SqlParameter("@UpdateBy", SqlDbType.Int,4),
                new SqlParameter()
			};

            parameters[0].Value = configurationId;
            parameters[1].Value = service;
            parameters[2].Value = type;
            parameters[3].Value = value;
            parameters[4].Value = updateBy;
            parameters[5].Direction = ParameterDirection.ReturnValue;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure, procedureName, parameters );

                return ( (int)( parameters[5].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion

        #region UpdateStatus

        /// <summary>
        /// public bool Update(int configurationId, int status, int updateBy)
        /// </summary>
        /// <param name="configurationId">configurationId</param>
        /// <param name="status">status</param>
        /// <param name="updateBy">updateBy</param>
        /// <returns>bool successful</returns>
        public static bool Update( int configurationId, int status, int updateBy )
        {
            string procedureName = "SYS_ServiceConfiguration_UpdateStatus";

            SqlParameter[] parameters = {
                new SqlParameter("@ConfigurationId", SqlDbType.Int, 4),
                new SqlParameter("@Status", SqlDbType.Int, 4),
                new SqlParameter("@UpdateBy", SqlDbType.Int, 4),
                new SqlParameter()
			};

            parameters[0].Value = configurationId;
            parameters[1].Value = status;
            parameters[2].Value = updateBy;
            parameters[3].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure, procedureName, parameters );

                return ( (int)( parameters[3].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion
    }
}

